import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';

import { ref } from 'vue';
import { useRouter } from 'vue-router';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { asyncFindUrlById } from '#/api';
import { ExtendNoticeApi } from '#/api/basic/msg/extendNotice';
import {
  ActionEnum,
  DictEnum,
  FileBizTypeEnum,
  FileBucketEnum,
  RouteEnum,
} from '#/enums/commonEnum';
import { PermCode } from '#/enums/perm';
import {
  backendDict,
  createdTimeColumn,
  deleteButton,
  indexColumn,
  YES_NO_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.basic.msg.myMsg;

export function createCrudOptions(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  const selectedRowKeys = ref<string[]>([]);
  const { push } = useRouter();
  const menuName = RouteEnum.BASIC_MY_MSG_VIEW;
  return {
    crudOptions: {
      request: {
        pageRequest: ExtendNoticeApi.pageRequest,
        delRequest: ExtendNoticeApi.delRequest,
        infoRequest: ExtendNoticeApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: false },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: ExtendNoticeApi.remove,
            role: permCode.delete,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: { show: false },
          copy: { show: false },
          view: {
            show: hasPermission(permCode.edit),
            async click({ row }) {
              if (row.autoRead) {
                await ExtendNoticeApi.mark([row.id]);
              }
              push({
                name: menuName,
                params: { type: ActionEnum.VIEW, id: row.id },
              });
            },
          },
          remove: { show: hasPermission(permCode.delete) },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        remindMode: {
          title: $t('basic.msg.extendNotice.remindMode'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.NoticeRemindModeEnum),
          search: { show: true },
          column: { width: 120 },
        },
        isRead: {
          title: $t('basic.msg.extendNotice.isRead'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          search: { show: true },
          form: { show: false },
          column: { width: 100 },
        },
        title: {
          title: $t('basic.msg.extendNotice.title'),
          type: 'text',
          search: { show: true },
        },
        author: {
          title: $t('basic.msg.extendNotice.author'),
          type: 'text',
          search: { show: true },
        },
        content: {
          title: $t('basic.msg.extendNotice.content'),
          type: 'editor-wang5',
          viewForm: { ignoreUseCellComponent: true },
          form: {
            col: { span: 24 },
            component: {
              id: '1', // 当同一个页面有多个editor时，需要配置不同的id
              uploader: {
                type: 'form',
                param: {
                  bucket: FileBucketEnum.public,
                  bizType: FileBizTypeEnum.EXTEND_MSG_CONTENT,
                },
                buildUrl: async (res: any) => {
                  if (res.file) {
                    return null;
                  }
                  const file = await asyncFindUrlById(res.url);
                  return file.data;
                },
              },
            },
          },
          column: { show: false },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
